package com.itheima.travel.web;



import com.github.pagehelper.PageInfo;
import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.FavoriteVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.FavoriteService;
import com.itheima.travel.utils.ExceptionsUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 *
 */
@RestController
@RequestMapping // 用来声明控制器类,且类下方法的返回值都转成Json字符串的格式返回给浏览器
@Log4j2
@CrossOrigin // 跨域连接
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;
    @Value("${upload.webSite}")
    String webSite;

    // 添加收藏
    // 返回值是插入后的总数
    public ResponseWrap<Integer> addFavorite(@RequestBody FavoriteVo favoriteVo) throws ProjectException {
        try {
            Integer flag = favoriteService.addFavorite(favoriteVo);
            return ResponseWrap.<Integer>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("添加收藏:{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.ADD_FAVORITE_FAIL.getCode(),StatusEnum.ADD_FAVORITE_FAIL.getMsg());
        }
    }

    // 分页查询
    @PostMapping("favorite/{pageNum}/{pageSize}")
    public ResponseWrap<PageInfo<RouteVo>> findMyFavorite(@PathVariable Integer pageNum,@PathVariable Integer pageSize) throws ProjectException {
        try{
            PageInfo<RouteVo> myFavorite = favoriteService.findMyFavorite(pageNum, pageSize);
            return ResponseWrap.<PageInfo<RouteVo>>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(myFavorite)
                    .build();
        }catch (Exception e){
            log.error("分页查询我的收藏:{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FIND_MYFAVORITE_FAIL.getCode(),StatusEnum.FIND_MYFAVORITE_FAIL.getMsg());
        }
    }

    // 是否收藏
    @GetMapping("favorite/{routeId}")
    public ResponseWrap<Boolean> isFavorited(@PathVariable("routeId") Long routeId) throws ProjectException {
        FavoriteVo favoriteVo = FavoriteVo.builder().routeId(routeId).build();
        try {
            Boolean flag = favoriteService.isFavorited(favoriteVo);
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("是否收藏:{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.ISFAVORITED_FAIL.getCode(),StatusEnum.ISFAVORITED_FAIL.getMsg());
        }
    }
}
